/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import data.Requirement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.ejb.EJB;
import javax.ejb.MessageDrivenContext;
import javax.ejb.Stateful;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

/**
 *
 * @author stana
 */
@ManagedBean
@SessionScoped
public class RequirementBean implements Serializable
{

    @EJB
    private business.RequirementFacade ejbFacade;
    private Requirement requirement;
    private String place;//pro inputText z index.xhtml
    @Future(message = "Vyberte prosím datum z budoucnosti")
    private Date date;
    @Size(min = 9, max = 9, message = "Číslo musí mít devět cifer např. 123456789")
    @Pattern(regexp = "[0-9]+", message = "Číslo může obsahovat pouze číslice bez mezer")
    private String phoneNumber;
    private boolean firstStage;
    private String selectedPlace;//pro ulozeni ktere mesto bylo vybrano z nabidky
    private List foundPlaces;//mesta v nabidce v index.xhtml
    private YahooPlaceFinder placeFinder;
    private List<Place> placesList;//seznam do ktereho se ulozi mesta, ktere najde yahoo
    private String info;//informace o chybach
    private String validNumber;//cislo na ktere muze byt zaslana predpoved
    @NotNull(message="Musíte zadat validační kód")
    @Pattern(regexp = "[0-9]+", message = "Kód je pouze číselný")
    private String code;//kod zadany uzivatelem
    private int actualCode;//spravny kod
    private boolean haveCode=false;//rozhoduje jestli se ukaze tlacitko pro odeslani kodu

    /** Creates a new instance of RequirementBean */
    public RequirementBean()
    {
        placeFinder = new YahooPlaceFinder();
        firstStage = true;
    }

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }

    public String getPhoneNumber()
    {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }

    public String getPlace()
    {
        return place;
    }

    public void setPlace(String place)
    {
        this.place = place;
    }

    public boolean isFirstStage()
    {
        return firstStage;
    }

    public void setFirstStage(boolean firstStage)
    {
        this.firstStage = firstStage;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public boolean isHaveCode()
    {
        return haveCode;
    }

    public void setHaveCode(boolean haveCode)
    {
        this.haveCode = haveCode;
    }



    public List getFoundPlaces()
    {
        foundPlaces = new ArrayList<SelectItem>();
        placesList = placeFinder.getPlaces(place);
        int i = 0;
        for (Place p : placesList)
        {
            foundPlaces.add(new SelectItem(i++, getPlaceName(p)));
        }
        return foundPlaces;
    }

    private String getPlaceName(Place p)
    {
        String ret = "";
        if (p.getLine1() == "")
        {
            ret = p.getLine2() + ", " + p.getLine4();
        } else
        {
            ret = p.getLine1() + ", " + p.getLine4();
        }
        return ret;
    }

    public void setFoundPlaces(List foundPlaces)
    {
        this.foundPlaces = foundPlaces;
    }

    public String getSelectedPlace()
    {
        return selectedPlace;
    }

    public void setSelectedPlace(String selectedPlace)
    {
        this.selectedPlace = selectedPlace;
    }

    public String getInfo()
    {
        return info;
    }

    public void setInfo(String info)
    {
        this.info = info;
    }

    public String find()
    {
        getFoundPlaces();
        if (foundPlaces.isEmpty())
        {
            info = "Nebylo nalezeno žádné místo, zkuste upravit vaše zadání";
        } else
        {
            firstStage = !firstStage;
            info = "";
        }
        return "/index";
    }

    public String changePlace()
    {
        firstStage = true;
        selectedPlace = null;
        foundPlaces = null;
        place = null;
        placesList = null;
        return "/index";
    }

    public String confirm()
    {
        if(code==null||code.isEmpty())
        {
            info="Nezadali jste validačni kod, klikněte prosím na tlačítko zaslat kód";
        }
        else if(actualCode == Integer.parseInt(code) && phoneNumber.equals(validNumber))
        {
            Place p = placesList.get(Integer.parseInt(selectedPlace));
            requirement = new Requirement(date, Long.valueOf(phoneNumber), getPlaceName(p), p.getLatitude(), p.getLongitude(), "tomorrow", 0);
            ejbFacade.edit(requirement);
            haveCode=false;
            info="";
            return "/pages/success";
        }
        else
        {
            info="Zadali jste chybný kód, nechte si zaslat nový";
            code="";
            haveCode=false;
        }
        return "/index";
    }

    public String sendCode()
    {
        SmsGateway gateway = new O2Gateway();
        actualCode = generateCode();
        validNumber = phoneNumber;
        gateway.sendSms("vas validacni kod:" + actualCode, phoneNumber);
        haveCode=true;
        info="";
        return "/index";
    }

    private int generateCode()
    {
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(10000);
        return randomInt;
    }
}
